new Vue({
    el: '#app',
    data() {
        return {
            nodeKey: '',
            tableData: [],
            treeData: [],
            Model_Name: '',
            a: true,
            loading: false,
            load: false,
            cloneTreeData: [],
            role: []
        }
    },
    mounted() {
        let ls_URL = window.location.href;
        this.nodeKey = getQueryObject(ls_URL, "nodeKey");// 获取 nodeKey 参数
        this.getData();
    },
    computed: {
        totalSwitch() {
            return function (row) {
                return !this.role.some(i => row[i.value] == '0');
            }
        }
    },
    methods: {
        check() {
            this.cloneTreeData = JSON.parse(JSON.stringify(this.tableData));
            let lArray_TreeData = this.cloneTreeData;
            this.treeData = this.Recursion(lArray_TreeData);
        },
        Recursion(lArray_TreeData) {
            lArray_TreeData.forEach(lJSONObject_Record => {//1级
                //获取对象中子节点
                let lJSONArray_Children = lJSONObject_Record.children;
                if (!lJSONArray_Children) {
                    let ls_ModleName = lJSONObject_Record.Model_Name;
                    if (!ls_ModleName.includes(this.Model_Name)) {
                        lArray_TreeData = lArray_TreeData.filter(i => i.Model_Number != lJSONObject_Record.Model_Number)
                    }
                } else {
                    lJSONArray_Children = this.Recursion(lJSONArray_Children);
                    //判断子节点长度，若子节点长度大于0则处理好的子节点更新
                    if (lJSONArray_Children.length > 0) {
                        lJSONObject_Record.children = lJSONArray_Children
                    } else {//将该子节点删除
                        lArray_TreeData = lArray_TreeData.filter(i => i.Model_Number != lJSONObject_Record.Model_Number);
                    }
                }
            })
            return lArray_TreeData;
        },
        async handleSubmit(row, total = false) {
            if (this.load) {
                row.loading = true;
                if (total) {
                    const is = this.totalSwitch(row);
                    this.role.forEach(i => {
                        row[i.value] = is ? '0' : '1'
                    });
                }
                try {
                    const data = await generalApi.generalQueryByTableNameWithOriginal({
                        Data: 'base_system_rights_user_model',
                        Condition: `Model_Number='${row.Model_Number}' AND User_Number='${this.nodeKey}'`
                    });
                    let action = 'Modify';
                    let lJSONObject_DataSet = new Object();
                    if (data.length == 0) { //添加
                        action = 'Add';
                        data.push({
                            Model_Number: row.Model_Number,
                            User_Number: this.nodeKey,
                        });
                    }
                    data[0].UR_Page = this.role.filter((i, id) => id < 4).map(i => row[i.value]).join('');
                    this.role.forEach(item => {
                        data[0][item.value] = row[item.value];
                    })
                    lJSONObject_DataSet[action] = {
                        base_system_rights_user_model: data
                    }
                    let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                    if (!lJSONObject_DataSet['Add']) {
                        lJSONObject_DataSet['Add'] = new Object();
                    }
                    lJSONObject_DataSet['Add']['base_operation_log'] = [{
                        Operation_Type: '0',
                        Operation_Path: getRelativePath(),
                        Operation_Description: '修改角色权限数据',
                        Operation_Main,
                        Operation_Interface: "generalSaveByRequestBody",
                        Operation_IP: getIpAddress()
                    }]
                    const res = await generalApi.generalSaveByRequestBody({
                        Data: lJSONObject_DataSet
                    });
                    if (res.Status == 'fail') {
                        this.$message.error('提交失败！请重试。');
                    }
                } catch (error) {
                    this.$message.error("系统错误，请联系开发者!")
                }
                row.loading = false;
            }
        },
        async getData() {
            this.loading = true;
            try {
                this.role = await generalApi.generalFilePath('role.json');
                const rights = await generalApi.generalQueryByTableName({
                    Data: 'base_system_rights_user_model',
                    Condition: `User_Number='${this.nodeKey}'`
                });
                const data = await generalApi.generalQueryForObtainingTrees({
                    TableName: 'base_system_model_list',
                    NodeCode: ''
                })
                this.configData(data, rights);
                this.tableData = data;
                this.treeData = data;
                this.loading = false;
                this.$nextTick(() => {
                    this.load = true;
                })
            } catch (error) {
                console.error(error);
                this.$message.error("系统错误，请联系开发者!")
            }
        },
        configData(data, rights) {
            data.forEach(item => {
                let obj = rights.filter(i => i.Model_Number == item.Model_Number)[0];
                this.role.forEach(row => {
                    item[row.value] = obj ? obj[row.value] : '0';
                })
                item.loading = false;
                if (item.children && item.children.length > 0) {
                    this.configData(item.children, rights);
                }
            })
        },
        handleBack() {
            window.location.href = 'index.html';
        },
    }
})